/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.database.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *
 * @author Valdigleis
 */
public abstract class GenericDAO {
    
    private Connection connection;
    
    protected GenericDAO(){
        this.connection = ConnectionDataBase.getConnection();
    }
    
    protected Connection getConnection(){
        return this.connection;
    }
    
    protected void save(String insertSql, Object... parametros){
        try{
            PreparedStatement pstmt = getConnection().prepareStatement(insertSql);
            for(int i = 0; i < parametros.length; i++){
                pstmt.setObject(i+1, parametros[i]);
            }
            pstmt.execute();
            pstmt.close();
        }catch(SQLException e){
            System.err.println("O erro foi na execeção de Salvar em GenericDAO");
            throw new RuntimeException(e);
        }
    }
    
    protected void update(String updateSql, Object... parametros){
        try{
            PreparedStatement pstmt = getConnection().prepareStatement(updateSql);
            for(int i = 0; i < parametros.length; i++){
                pstmt.setObject(i+1, parametros[i]);
            }
            pstmt.execute();
            pstmt.close();
        }catch(SQLException e){
            throw new RuntimeException(e);
        }
    }
    
    protected void delete(String deleteSql, Object... parametros){
        try{
            PreparedStatement pstmt = getConnection().prepareStatement(deleteSql);
            for(int i = 0; i < parametros.length; i++){
                pstmt.setObject(i+1, parametros[i]);
            }
            pstmt.execute();
            pstmt.close();
        }catch(SQLException e){
            throw new RuntimeException(e);
        }
    }
    
    protected void shutdown() throws SQLException{
        getConnection().createStatement().executeUpdate("SHUTDOWN");
    }
}
